package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行关联查询
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        //需求：王克晶所带班级的信息，列出老师的名字，老师的性别，班级名，所在楼层
        /*
        SELECT t.name,t.gender,c.name,c.floor
        FEOM teacher t,class c
        WHERE t.id=c.teacher_id
        AND t.name='王克晶';
         */
        try (Connection conn =  DBUtil.getConnection();){
            Statement  state = conn.createStatement();
            String sql = "SELECT t.name,t.gender,c.name,c.floor " +
                         "FROM teacher t,class c WHERE t.id=c.teacher_id " +
                         "AND t.name='王克晶'";
            ResultSet rs = state.executeQuery(sql);
            while (rs.next()){
                String tName = rs.getString("t.name");
                String tGender = rs.getString("t.gender");
                String cname = rs.getString("c.name");
                int cFloor = rs.getInt("c.floor");
                System.out.println(tName+"--"+tGender+"--"+cname+"--"+cFloor);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
